package leetcode;

import leetcode.entity.ListNode;

/**
 * 1,2,3,4,5  2
 * 5
 * @author:xufengxiang
 * @date 2023-04-03 19:39
 */
public class Code19 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode p = head;
        int length = 1;
        while(p.next != null){
            length ++;
            p = p.next;
        }
        //加一个虚假头节点，因为链表中任意一个节点都可能被删除。增加虚假头节点，简化问题
        ListNode dump = new ListNode(0,head);
        p = dump;
        for(int i=1;i<length-n+1;i++){
            p = p.next;
        }
        p.next = p.next.next;
        return dump.next;
    }


    public static void main(String[] args) {

    }

}
